using System.Collections;
using BCW.UI.Animation;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class TargetDetectorUI : MonoBehaviour
{
	[Header("Максимальная ширина прицела при слежении за целью (в процентах 0-1)")]
	public float maxDistanceAimZoneByLastTarget = 0.8f;

	public float offsetDistanceAimZoneByLastTarget = 50f;

	[Header("Расстояния определения и захвата цели")]
	public float maxFocusDistance;

	public float maxCatchDistance;

	[Header("Индикатор расстояния")]
	public GameObject targetDistanceIndicator;

	public UILabel targetDistanceLabel;

	[Header("Красный квадрат вокруг врага")]
	public UISprite targetFocusSprite;

	public UISprite targetCatchSprite;

	public UIAnimator targetFocusAnimator;

	public UIAnimator targetCatchAnimator;

	public GameObject titanAim;

	[Header("Полосочки зоны захвата для отладки")]
	public GameObject leftAimZoneBorder;

	public GameObject rightAimZoneBorder;

	[Header("Минимальный размер для квадрата захвата")]
	public int minFocusWidth;

	public int maxFocusWidth = 200;

	[Header("Риски и прицел")]
	public GameObject leftMargin;

	public GameObject rightMargin;

	public GameObject aim;

	public UISprite aimSprite;

	private Color defaultAimColor;

	[Header("Высоты сущностей для размера спрайтов захвата")]
	public int vehicleDefaultHeight;

	public int titanDefaultHeight;

	public int humanDefaultHeight;

	public bool showBorders;

	public UIRoot uiRoot;

	public Camera mainCamera;

	private Vector3 screenCenter = new Vector3((float)Screen.width / 2f, (float)Screen.height / 2f, 0f);

	public static TargetDetectorUI instance;

	private Tweener hitAnimation;

	private Coroutine _enemyCatchedForHomingMissleCoroutine;

	private TweenSettings _catchAlpha;

	private TweenSettings _focusScale;

	private float _defaultCatchAlpahOffset;

	private float _defaultFocusScaleTime;

	public TweenSettings CatchAlpha
	{
		get
		{
			return _catchAlpha;
		}
	}

	public TweenSettings FocusScale
	{
		get
		{
			return _focusScale;
		}
	}

	public float DefaultCatchAlpahOffset
	{
		get
		{
			return _defaultCatchAlpahOffset;
		}
	}

	public float DefaultFocusScaleTime
	{
		get
		{
			return _defaultFocusScaleTime;
		}
	}

	private void Awake()
	{
		instance = this;
		aimSprite = aim.GetComponent<UISprite>();
		defaultAimColor = aimSprite.color;
		if (uiRoot == null)
		{
			uiRoot = Object.FindObjectOfType<UIRoot>();
		}
		if (mainCamera == null)
		{
			mainCamera = Camera.main;
		}
		SaveDefaultDelayAimValues();
	}

	public Vector3 PositionOnScreen(Vector3 target)
	{
		Vector3 vector = mainCamera.WorldToScreenPoint(target);
		vector -= screenCenter;
		return vector * ((float)uiRoot.activeHeight / (float)Screen.height);
	}

	public Vector3 NGUIToUnityScreen(Vector3 target)
	{
		target /= (float)uiRoot.activeHeight / (float)Screen.height;
		target += screenCenter;
		return target;
	}

	public bool TargetIsOnScreen(Vector3 target)
	{
		Vector3 vector = PositionOnScreen(target);
		return Mathf.Abs(vector.x) < (float)Screen.width && Mathf.Abs(vector.y) < (float)Screen.height && vector.z >= 0f;
	}

	public float GetAimZoneWidthForLastTarget(float aimZoneWidth, Vector3 enemyPosition)
	{
		float b = Mathf.Abs(instance.PositionOnScreen(enemyPosition).x) + offsetDistanceAimZoneByLastTarget;
		return Mathf.Min((float)Screen.width / 2f * maxDistanceAimZoneByLastTarget, Mathf.Max(aimZoneWidth, b));
	}

	public bool IsAimInFocusZone()
	{
		Bounds bounds = targetFocusSprite.CalculateBounds();
		float num = Mathf.Abs(aimSprite.transform.localPosition.x - targetFocusSprite.transform.localPosition.x);
		return num < bounds.extents.x;
	}

	public void UpdateFocusOnTarget(Mortal mortalTarget, float distance, bool newTarget)
	{
		if (newTarget)
		{
			EnableAim(false);
			EnableHomingAim(false);
			targetFocusSprite.enabled = false;
		}
		else
		{
			targetFocusSprite.enabled = true;
		}
		if (mortalTarget == null)
		{
			DisableAllInterface();
			return;
		}
		Vector3 positionForChargeHit = mortalTarget.GetPositionForChargeHit();
		Vector3 vector = PositionOnScreen(positionForChargeHit);
		float num = WeaponTargetDetector.instance.GetAimZoneWidthForTarget(distance);
		if (!WeaponTargetDetector.instance.fixedMaxZoneWidth)
		{
			num = GetAimZoneWidthForLastTarget(num, mortalTarget.transform.position);
		}
		if (distance > maxFocusDistance)
		{
			DisableAllInterface();
			return;
		}
		EnableAim(true);
		targetFocusSprite.transform.localPosition = vector;
		if (WeaponTargetDetector.instance.homingAim)
		{
			bool flag = false;
			flag = distance < WeaponTargetDetector.instance.maxDistance;
			EnableHomingAim(flag);
			if (flag)
			{
				targetCatchSprite.transform.localPosition = vector;
				leftMargin.transform.localPosition = Vector3.left * num;
				rightMargin.transform.localPosition = Vector3.right * num;
			}
			if (flag)
			{
				targetDistanceIndicator.SetActive(true);
				aim.SetActive(true);
				aim.transform.localPosition = Vector3.up * vector.y;
				targetDistanceLabel.text = distance.ToString("F0");
				int num2 = 0;
				num2 = ((mortalTarget is TitanBehavior) ? titanDefaultHeight : ((mortalTarget is EntityBehavior) ? vehicleDefaultHeight : ((!(mortalTarget is PlayerBehavior)) ? mortalTarget.GetBodyHeight() : humanDefaultHeight)));
				if (num2 == 0)
				{
					num2 = mortalTarget.GetBodyHeight();
				}
				Vector3 vector2 = vector;
				Vector3 vector3 = PositionOnScreen(positionForChargeHit + Vector3.up * num2);
				int width = (int)Mathf.Clamp(Mathf.Abs(vector2.y - vector3.y), minFocusWidth, maxFocusWidth);
				targetFocusSprite.width = width;
				targetCatchSprite.width = width;
			}
			else
			{
				targetDistanceIndicator.SetActive(false);
				aim.SetActive(false);
			}
		}
		else
		{
			EnableHomingAim(false);
		}
	}

	public void HitAnimation()
	{
		if (hitAnimation != null)
		{
			HOTween.Kill(hitAnimation);
		}
		aimSprite.color = Color.red;
		hitAnimation = HOTween.To(aimSprite, 0.2f, new TweenParms().Prop("color", Color.white).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			aimSprite.color = defaultAimColor;
		}));
	}

	public void DisableAllInterface()
	{
		targetFocusSprite.gameObject.SetActive(false);
		targetCatchSprite.gameObject.SetActive(false);
		targetDistanceIndicator.SetActive(false);
		if (WeaponTargetDetector.instance != null)
		{
			if (_enemyCatchedForHomingMissleCoroutine != null)
			{
				StopCoroutine(_enemyCatchedForHomingMissleCoroutine);
				_enemyCatchedForHomingMissleCoroutine = null;
			}
			WeaponTargetDetector.instance.enemyCatchedForHomingMissle = false;
		}
		aim.SetActive(false);
		leftMargin.SetActive(false);
		rightMargin.SetActive(false);
	}

	private void EnableHomingAim(bool enable)
	{
		if (!enable && targetCatchSprite.gameObject.activeSelf)
		{
			EnableAim(false);
		}
		if (enable && _enemyCatchedForHomingMissleCoroutine == null)
		{
			_enemyCatchedForHomingMissleCoroutine = StartCoroutine(EnemyCatchedForHomingMissleCoroutine());
		}
		if (!enable)
		{
			if (_enemyCatchedForHomingMissleCoroutine != null)
			{
				StopCoroutine(_enemyCatchedForHomingMissleCoroutine);
				_enemyCatchedForHomingMissleCoroutine = null;
			}
			WeaponTargetDetector.instance.enemyCatchedForHomingMissle = false;
		}
		targetCatchSprite.gameObject.SetActive(enable);
		leftMargin.SetActive(enable);
		rightMargin.SetActive(enable);
	}

	private IEnumerator EnemyCatchedForHomingMissleCoroutine()
	{
		yield return new WaitForSeconds(_catchAlpha.offset);
		WeaponTargetDetector.instance.enemyCatchedForHomingMissle = true;
	}

	private void SaveDefaultDelayAimValues()
	{
		if (targetCatchAnimator.sequence.Count > 0)
		{
			foreach (TweenSettings tweenSetting in targetCatchAnimator.sequence[0].tweenSettings)
			{
				if (tweenSetting.animationType.Equals(AnimType.Alpha))
				{
					_catchAlpha = tweenSetting;
					_defaultCatchAlpahOffset = _catchAlpha.offset;
				}
			}
		}
		if (targetFocusAnimator.sequence.Count <= 0)
		{
			return;
		}
		foreach (TweenSettings tweenSetting2 in targetFocusAnimator.sequence[0].tweenSettings)
		{
			if (tweenSetting2.animationType.Equals(AnimType.Scale))
			{
				_focusScale = tweenSetting2;
				_defaultFocusScaleTime = _focusScale.time;
			}
		}
	}

	private void EnableAim(bool enable)
	{
		targetFocusSprite.gameObject.SetActive(enable);
	}
}
